
const mssql = require("mssql");
const mongodbClient = require("mongodb").MongoClient;
 

// mongodbClient.connect("mongodb://localhost:27017").then((client) => {
//     let db = client.db("kaifang2000w");
//     let person = db.collection("person");
//     person.insert({
//         name: "顾世豪",
//         sex: 1,
//         age: 24,
//         address: "浙江省杭州市"
//     });
// })
// .catch((res) => {
//     console.log(2);
// });


async function fenye (req, num) {
    let result = await req.input("num", mssql.Int, num).execute("fenye");
    return result; 
}

(async () => {
    try {
        const pool = await mssql.connect('mssql://sa:gushihao@localhost/kaifang2000w');
        const req = await pool.request();

        const client = await mongodbClient.connect("mongodb://localhost:27017");
        const db = client.db("kaifang2000w");
        const person = db.collection("person");

        // let result = await fenye(req, 0);
        // await person.insertMany(result.recordset);    

        for (let num = 0; true; num++) {
            console.log(num + "：开始");
            let result = await fenye(req, num);
            let data = result.recordset;
            if (data.length < 1) {
                console.log("全部结束了！");
                break;
            }
            await person.insertMany(result.recordset);    
            console.log(num + "：结束");
        }
    } catch (err) {
        console.log(err.message);
        // ... error checks
    }
})();